# ------------------------------------------------------------------------------
# libpvr SConscript
# ------------------------------------------------------------------------------

from SCons import Script

import BuildSupport

import os, sys

# ------------------------------------------------------------------------------

# scons loads data into the "env" variable
env = None
Script.Import("env")

# ------------------------------------------------------------------------------
# Build libpvr
# ------------------------------------------------------------------------------

libEnv = env.Clone()
BuildSupport.setupLibBuildEnv(libEnv)

files = Script.Glob(os.path.join(BuildSupport.buildDir(libEnv), "*.cpp"))
files += Script.Glob(os.path.join(BuildSupport.buildDir(libEnv), "Lights", "*.cpp"))
files += Script.Glob(os.path.join(BuildSupport.buildDir(libEnv), "Noise", "*.cpp"))
files += Script.Glob(os.path.join(BuildSupport.buildDir(libEnv), "Occluders", "*.cpp"))
files += Script.Glob(os.path.join(BuildSupport.buildDir(libEnv), "Primitives", "*.cpp"))
files += Script.Glob(os.path.join(BuildSupport.buildDir(libEnv), "Primitives/Rasterization", "*.cpp"))
files += Script.Glob(os.path.join(BuildSupport.buildDir(libEnv), "Primitives/Instantiation", "*.cpp"))
files += Script.Glob(os.path.join(BuildSupport.buildDir(libEnv), "Raymarchers", "*.cpp"))
files += Script.Glob(os.path.join(BuildSupport.buildDir(libEnv), "RaymarchSamplers", "*.cpp"))
files += Script.Glob(os.path.join(BuildSupport.buildDir(libEnv), "Volumes", "*.cpp"))

# Declare library
sharedLibPath = os.path.join(BuildSupport.buildDir(libEnv), BuildSupport.pvrName)
dyLib = libEnv.SharedLibrary(sharedLibPath, files)
# stLib = libEnv.Library(sharedLibPath, files)

# Set up install area
headerInstall = BuildSupport.setupHeaderInstall(libEnv)
# stLibInstall = libEnv.Install(os.path.join(BuildSupport.installDir(), "lib"), [stLib])

# Set up the dynamic library properly on OSX
dylibInstall = None
if sys.platform == "darwin":
    dylibName = os.path.basename(str(dyLib[0]))
    dylibInstallPath = os.path.abspath(os.path.join(BuildSupport.installDir(libEnv), "lib", dylibName))
    # Create the builder
    dylibEnv = env.Clone()
    dylibBuilder = Script.Builder(action = BuildSupport.setDylibInternalPath,
                           suffix = ".dylib", src_suffix = ".dylib")
    dylibEnv.Append(BUILDERS = {"SetDylibPath" : dylibBuilder})
    # Call builder
    dyLibInstall = dylibEnv.SetDylibPath(dylibInstallPath, dyLib)
else:
    dyLibInstall = libEnv.Install(os.path.join(BuildSupport.installDir(libEnv), "lib"), [dyLib])

# ------------------------------------------------------------------------------
# Set up python library
# ------------------------------------------------------------------------------

pyEnv = env.Clone()
BuildSupport.setupPyEnv(pyEnv)
pyFiles = Script.Glob(os.path.join(BuildSupport.buildDir(pyEnv), BuildSupport.pythonVer(), "*.cpp"))
pyLib = BuildSupport.defineBoostPythonModule(BuildSupport.pvrName, pyFiles, pyEnv)
pyFiles = Script.Glob(os.path.join(BuildSupport.python, BuildSupport.pvrName, "*.py"))
pyLibInstall = env.Install(os.path.join(BuildSupport.installDir(pyEnv), BuildSupport.pythonVer(), BuildSupport.pvrName), pyLib)
pyFilesInstall = env.Install(os.path.join(BuildSupport.installDir(pyEnv), BuildSupport.pythonVer(), BuildSupport.pvrName), pyFiles)
env.Depends(pyLib, dyLibInstall)
env.Depends(pyLib, headerInstall)

# ------------------------------------------------------------------------------
# Install
# ------------------------------------------------------------------------------

pyInstall = BuildSupport.installPyLib(env, pyLib, pyFiles)
Script.Depends(pyInstall, pyLibInstall)
Script.Depends(pyInstall, pyFilesInstall)

# ------------------------------------------------------------------------------
# Aliases
# ------------------------------------------------------------------------------

env.Alias("lib", [headerInstall, dyLibInstall])
env.Alias("pylib", [pyLibInstall, pyFilesInstall])
env.Alias("pyinstall", pyInstall)

# ------------------------------------------------------------------------------
# Help
# ------------------------------------------------------------------------------

try:
    from SCons.Node.Alias import default_ans
except ImportError:
    pass
else:
    aliases = default_ans.keys()
    aliases.sort()
    env.Help('\n')
    env.Help('Recognized targets:\n')
    for alias in aliases:
        env.Help('    %s\n' % alias)

